﻿Imports System.Activities
Imports System.ComponentModel
Imports System.Activities.Core.Presentation
Imports System.Activities.Presentation.Metadata
Imports System.Drawing

Public Class MessageBoxActivity
    Inherits AsyncCodeActivity
    Implements IRegisterMetadata

#Region "プロパティ"
    Public Property Title As InArgument(Of String) = My.Application.Info.Title
    Public Property Icon As MessageBoxImage = MessageBoxImage.Information
    Public Property Buttons As MessageBoxButton = MessageBoxButton.OK

    Public Property Messages As InArgument(Of String)

    Public Property Results As OutArgument(Of MessageBoxResult)
#End Region

    Private Delegate Function AsyncMessageBoxDelegate(ByVal titleString As String, ByVal dialogIcon As MessageBoxImage,
                                                                               ByVal dialogButtons As MessageBoxButton, ByVal message As String) As MessageBoxResult

    Public Sub New()
        Me.DisplayName = GetResourceStrings("ActMBDisplayName", "メッセージボックス表示")
    End Sub

    Protected Overrides Function BeginExecute(context As System.Activities.AsyncCodeActivityContext, callback As System.AsyncCallback, state As Object) As System.IAsyncResult
        Dim asyncMessageBoxDel = New AsyncMessageBoxDelegate(AddressOf DisplayDialog)
        context.UserState = asyncMessageBoxDel

        Dim titleValue = context.GetValue(Me.Title)
        Dim iconValue = Me.Icon
        Dim buttonValue = Me.Buttons
        Dim messageValue = context.GetValue(Me.Messages)

        Return asyncMessageBoxDel.BeginInvoke(titleValue, iconValue, buttonValue, messageValue, callback, state)
    End Function

    Protected Overrides Sub EndExecute(context As System.Activities.AsyncCodeActivityContext, result As System.IAsyncResult)
        Dim asyncMessageBoxDel = TryCast(context.UserState, AsyncMessageBoxDelegate)
        Dim resultValue = asyncMessageBoxDel.EndInvoke(result)
        context.SetValue(Me.Results, resultValue)
    End Sub

    Private Function DisplayDialog(ByVal titleString As String, ByVal dialogIcon As MessageBoxImage, ByVal dialogButtons As MessageBoxButton, ByVal message As String) As MessageBoxResult
        Return MessageBox.Show(message, titleString, dialogButtons, dialogIcon)
    End Function

#Region "メタデータ"

    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim metadata As New DesignerMetadata
        metadata.Register()

        Dim builder As New AttributeTableBuilder
        builder.AddCustomAttributes(GetType(MessageBoxActivity),
                            New DesignerAttribute(GetType(MessageBoxActivityDesigner)),
                            New ToolboxBitmapAttribute("Images/Activity/messagebox.png")
                            )
        builder.AddCustomAttributes(GetType(MessageBoxActivity), "Title",
                            New CategoryAttribute(GetResourceStrings("ACtMBPropertyCategory", "処理の設定")),
                            New DisplayNameAttribute(GetResourceStrings("ACtMBTitleProperty", "タイトル")),
                            New DescriptionAttribute(GetResourceStrings("ACtMBTitleDescription", "ダイアログのタイトルを設定します。")))
        builder.AddCustomAttributes(GetType(MessageBoxActivity), "Icon",
                            New CategoryAttribute(GetResourceStrings("ACtMBPropertyCategory", "処理の設定")),
                            New DisplayNameAttribute(GetResourceStrings("ACtMBIconProperty", "アイコン")),
                            New DescriptionAttribute(GetResourceStrings("ACtMBIconDescription", "ダイアログで表示するアイコンを設定します。")))
        builder.AddCustomAttributes(GetType(MessageBoxActivity), "Buttons",
                            New CategoryAttribute(GetResourceStrings("ACtMBPropertyCategory", "処理の設定")),
                            New DisplayNameAttribute(GetResourceStrings("ACtMBButtonsProperty", "アイコン")),
                            New DescriptionAttribute(GetResourceStrings("ACtMBButtonsDescription", "ダイアログで表示するアイコンを設定します。")))
        builder.AddCustomAttributes(GetType(MessageBoxActivity), "Messages",
                            New CategoryAttribute(GetResourceStrings("ACtMBPropertyCategory", "処理の設定")),
                            New DisplayNameAttribute(GetResourceStrings("ACtMBMessagesProperty", "アイコン")),
                            New DescriptionAttribute(GetResourceStrings("ACtMBMessagesDescription", "ダイアログで表示するアイコンを設定します。")))
        builder.AddCustomAttributes(GetType(MessageBoxActivity), "Results",
                    New CategoryAttribute(GetResourceStrings("ACtMBPropertyCategory", "処理の設定")),
                    New DisplayNameAttribute(GetResourceStrings("ACtMBResultsProperty", "アイコン")),
                    New DescriptionAttribute(GetResourceStrings("ACtMBResultsDescription", "ダイアログで表示するアイコンを設定します。")))

        MetadataStore.AddAttributeTable(builder.CreateTable)
    End Sub

#End Region

#Region "その他"

    ''' <summary>リソースより文字列の取得</summary>
    Private Shared Function GetResourceStrings(ByVal resourceName As String, ByVal defaultStrings As String)
        Dim locMes = CStr(Application.Current.TryFindResource(resourceName))
        If (locMes Is Nothing) OrElse (locMes.Trim = "") Then locMes = defaultStrings
        Return locMes
    End Function

#End Region

End Class
